import Blender
import GameLogic
from Blender import Material
import GameKeys

ob = Blender.Object.Get('Cylinder')
co = GameLogic.getCurrentController()
obj = co.getOwner()
sens = co.getSensor('touchSens')
touchObj = sens.getHitObject()
#greenMat = Blender.Material.Get('green')
#stageMat = Blender.Material.Get('Lvl1Stage_Mat')

go = False

keysens = co.getSensor('spaceSens')
keylist = keysens.getPressedKeys()
for x in keylist:
	if x[1] == 1:
		if x[0] == GameKeys.SPACEKEY:
			#print 'key down!'
			go = True
		
if go:
	
	scn = GameLogic.getCurrentScene()
	
	if (obj.name != 'NoneType') and (touchObj.name != 'NoneType') and (obj.name != '') and (touchObj.name != ''):
		print obj.name + ' collided with ' + touchObj.name

	#ob2 = Blender.Object.Get(touchObj.name[2:])
	obj.state = (obj.state + 1) % 3
	
	if obj.state == 1:
		#touchObj.setVisible(False)
		
		hitList = sens.getHitObjectList()
		for item in hitList:
			print 'evaluating ' + item.name
			if item.name.find('Green') != -1:
				#touchObjBombGreen = item
				#print 'green bomb: ' + touchObjBombGreen.name
				obj.greenSave = item.name
			
			if item.name.find('Red') != -1:
				#touchObjBombRed = item
				#print 'red bomb: ' + touchObjBombRed.name
				obj.redSave = item.name

			if item.name.find('OBCube') != -1:
				obj.spaceSave = item.name
			
		print 'our saved green object is: ' + obj.greenSave
		print 'our saved red object is: ' + obj.redSave
		print 'our saved space object is: ' + obj.spaceSave
		
		scn.getObjectList()[obj.spaceSave].setVisible(False)
		print 'is spaceSave visible? ' + str(scn.getObjectList()[obj.spaceSave].visible)
		
		scn.getObjectList()[obj.greenSave].setVisible(True)
		print 'is greenSave visible? ' + str(scn.getObjectList()[obj.greenSave].visible)
		
		scn.getObjectList()[obj.redSave].setVisible(False)
		print 'is redSave visible? ' + str(scn.getObjectList()[obj.redSave].visible)
	elif obj.state == 2:
		#touchObj.setVisible(False)
		print 'our saved space object is: ' + obj.spaceSave
		print 'our saved green object is: ' + obj.greenSave
		print 'our saved red object is: ' + obj.redSave
		
		scn.getObjectList()[obj.spaceSave].setVisible(False)
		scn.getObjectList()[obj.greenSave].setVisible(False)
		scn.getObjectList()[obj.redSave].setVisible(True)
		
		#Blender.Object.Get(obj.spaceSave).visible = False
		#Blender.Object.Get(obj.redSave).visible = True
		#Blender.Object.Get(obj.greenSave).visible = False
		#Blender.Redraw()
		
		# Let each cube find out if it needs to kill itself
		#killAct = co.getActuator('killAct')
		#GameLogic.addActiveActuator(killAct, True)	
		obj.timer = 0.0		# this forces 'timedExplode' to execute
					
		
